package com.ql.gamelogm.utils;


import javax.servlet.http.HttpServletRequest;
import java.net.*;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.List;

public class IpUnit {

    public static String getIpAddr() {
        List<String> ipList = new ArrayList<String>();
        String getIp = "";
        String splitIp="";
        try {
            //ip地址例如：DESKTOP-4T13TPK/192.168.206.1
            getIp = InetAddress.getLocalHost().toString();
            //分割/取后面ipv4地址
            splitIp=getIp.substring(getIp.indexOf("/")+1);
            //取其他所有ipv4地址
            Enumeration<NetworkInterface> networkInterfaces = NetworkInterface.getNetworkInterfaces();
            NetworkInterface networkInterface;
            Enumeration<InetAddress> inetAddresses;
            InetAddress inetAddress;
            String ip;
            while (networkInterfaces.hasMoreElements()) {
                networkInterface = networkInterfaces.nextElement();
                inetAddresses = networkInterface.getInetAddresses();
                while (inetAddresses.hasMoreElements()) {
                    inetAddress = inetAddresses.nextElement();
                    if (inetAddress != null && inetAddress instanceof Inet4Address) { // IPV4
                        ip = inetAddress.getHostAddress();
                        ipList.add(ip);
                    }
                }
            }
        } catch (UnknownHostException e) {
            e.printStackTrace();
        } catch (SocketException e) {
            e.printStackTrace();
        }
        return splitIp;
    }

}